package com.shop.manager.controller;

//import com.shop.manager.entity.Goods;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Goods;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.GoodsService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @Description 产品增删改查
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * @Description 添加产品信息
     * @param goods 产品对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Goods 对象") Goods goods) {
        try {
            goodsService.addGoods(goods);
            return HttpResult.ok("添加产品成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加产品失败!");
    }

    /**
     * @Description 批量删除产品信息
     * @param ids 产品id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody(required=false) Long[] ids){
        try {
            goodsService.deleteGoods(Arrays.asList(ids));
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改删除产品信息
     * @param goods 修改的产品对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Goods 对象") Goods goods) {
        try {
            goodsService.updateGoods(goods);
            return HttpResult.ok("修改产品成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改产品失败!");
    }

    /**
     * @Description 根据id查询产品信息
     * @param id 产品id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(goodsService.getByGoodsId(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param goods 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) Goods goods) {
        try {
            //分页查询
            PageResult result = goodsService.getAllGoods(pageNum,size,goods);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询产品异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = goodsService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询产品
     * @param goods 产品的对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "GoodsDesc 对象") Goods goods) {
        return HttpResult.ok(goodsService.findByWhere(goods));
    }

    /***
     * @Description 修改产品状态
     * @param ids:要修改的商品ID
     * @param status:修改的状态
     * @return
     */
    @RequestMapping("/updateStatus")
    public HttpResult updateStatus(@RequestBody Long[] ids, @RequestParam("status") String status){
        int count = goodsService.updateStatus(Arrays.asList(ids), status);
        return HttpResult.ok(count);
    }


    /**
     * 根据模板id查询规格
     * @param typeTemplateId 模板id
     * @return
     */
    @RequestMapping("/querySpecificationOption")
    public HttpResult querySpecificationOption(@RequestParam("typeTemplateId") Long typeTemplateId){
        Map<String, Object> map = goodsService.querySpecificationOption(typeTemplateId);
        return HttpResult.ok(map);
    }

    /**
     * 查询全部产品
     * @return
     */
    @RequestMapping("/all")
    public HttpResult all(){
        HttpResult result = goodsService.findAll();
        return result;
    }




}
